package com.thinkcoder.exception;


import com.thinkcoder.protocol.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

@Slf4j
@ControllerAdvice(annotations = {RestController.class, Controller.class})
public class GlobalExceptionHandler {

    @ResponseBody
    @ExceptionHandler(UnAuthException.class)
    @ResponseStatus(HttpStatus.UNAUTHORIZED)
    public Result handlerUnAuthException(UnAuthException e){
        log.error(e.getMessage());
        return Result.buildFailure(e.getCode(),e.getMessage());
    }

    @ResponseBody
    @ExceptionHandler(ServiceException.class)
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    public Result handlerException(ServiceException e){
        log.error(e.getMessage());
        return Result.buildFailure(e.getCode(),e.getMessage());
    }

}
